package com.neusoft.neuiotms.dcom.repository.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.model.QStopServiceNotice;
import com.neusoft.neuiotms.dcom.model.StopServiceNotice;
import com.neusoft.neuiotms.dcom.repository.CustomizedStopServiceNoticeRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;

public class CustomizedStopServiceNoticeRepositoryImpl extends AbstractRepository
        implements CustomizedStopServiceNoticeRepository {

    @Override
    public List<StopServiceNotice> search(final Searchable searchable) {
        final QStopServiceNotice qStopService = QStopServiceNotice.stopServiceNotice;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<StopServiceNotice> query = this.query().selectFrom(qStopService);
        query.where(where);
        return query.fetch();
    }

    @Override
    public Page<StopServiceNotice> search(final Pageable pageable, final Searchable searchable) {
        final QStopServiceNotice qStopService = QStopServiceNotice.stopServiceNotice;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<StopServiceNotice> query = this.query().selectFrom(qStopService);
        query.where(where);
        return super.search(query, pageable);
    }

    private BooleanBuilder buildWhere(final Searchable searchable) {
        final BooleanBuilder where = new BooleanBuilder();
        final QStopServiceNotice qAsset = QStopServiceNotice.stopServiceNotice;

        if (!StringUtils.isEmpty(searchable.getStrValue("name"))) {
            where.or(super.containsStr(qAsset.name, searchable, "name"));
        }
        if (!StringUtils.isEmpty(searchable.getStrValue("code"))) {
            where.or(super.containsStr(qAsset.code, searchable, "code"));
        }

        return where;
    }

    @Override
    protected Class<?> getModelClass() {
        return StopServiceNotice.class;
    }

}
